package filter;

import javax.servlet.*;
import java.io.IOException;

/**
 * @author Mark
 * @date 2020/5/26 22:12
 * @description EncodingFilter
 */
public class EncodingFilter implements Filter {

    /**
     * 跟著服務器走
     *
     * @param filterConfig FilterConfig
     * @throws ServletException 異常
     */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("init");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        System.out.println("before");
        // chain 都會執行
        chain.doFilter(request, response);
        System.out.println("after");
    }

    /**
     * 跟著服務器銷毀
     */
    @Override
    public void destroy() {
        System.out.println("destroy");
    }
}
